//
//  OO_HomeViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/26.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_HomeViewController.h"
#import "SDCycleScrollView.h"
#import "OO_HomeCell.h"
#import "OO_WebViewController.h"

@interface OO_HomeViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *HeadView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation OO_HomeViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    
    return _dataSource;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf
    /*只有一组数据，不要上拉加载*/
    [self.tableView setRefreshWithHeaderBlock:^{
        [weakSelf loadData ];
    } footerBlock:nil];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView setupEmptyDataText:@"没有数据,点击重试" verticalOffset:40 emptyImage:[UIImage imageNamed:@"nodata"] tapBlock:^{
        [weakSelf.tableView.mj_header beginRefreshing ];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - NetWork
-(void)loadData{
    NSString *urlString = @"Home#!method=get";
    [PPNetworkHelper GET:[NSString stringWithFormat:@"%@%@",HTTP,urlString] parameters:nil success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        [self.dataSource removeAllObjects];
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSArray * bannerArr = dict[@"data"][@"banner"];
        [self initViews:bannerArr];
        [self.dataSource addObjectsFromArray:[OO_GoodsModel mj_objectArrayWithKeyValuesArray:dict[@"data"][@"goods"]]];
        [self.tableView headerEndRefreshing];
        [self.tableView footerNoMoreData];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:@"网络请求失败"];
        [self.tableView headerEndRefreshing];
    }];
    
    
}
#pragma mark - UI
-(void)initViews:(NSArray *)imageArr{
    for (UIView *vi in self.HeadView.subviews) {
        if ([vi isKindOfClass:[SDCycleScrollView class]]) {
                [vi removeFromSuperview];
        }
    }
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160) shouldInfiniteLoop:YES imageNamesGroup:nil];
    cycleScrollView.imageURLStringsGroup = imageArr;
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    cycleScrollView.bannerImageViewContentMode =  UIViewContentModeScaleAspectFill;
    [self.HeadView addSubview:cycleScrollView];
    
    
}
#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
}
#pragma mark - TableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OO_HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OO_HomeCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40 + ((SCREEN_WIDTH - 40) / 3.0);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    OO_DetailViewController *vc = [OO_DetailViewController new];
    vc.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - Event

- (IBAction)aboutUs:(id)sender {
    OO_WebViewController *vc  = [OO_WebViewController new];
    vc.title = @"关于我们";
    vc.imageName = @"关于我们";
    [self.navigationController pushViewController:vc animated:1];
}
- (IBAction)qiyeWenHua:(id)sender {
    OO_WebViewController *vc  = [OO_WebViewController new];
    vc.title = @"企业文化";
       vc.imageName = @"企业文化";
    [self.navigationController pushViewController:vc animated:1];
}
- (IBAction)mendianfenbu:(id)sender {
    OO_WebViewController *vc  = [OO_WebViewController new];
    vc.title = @"门店分布";
       vc.imageName = @"门店分布";
    [self.navigationController pushViewController:vc animated:1];
}
@end
